package com.itheima.controller;
import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.ResultMessage;
import com.itheima.pojo.TCategoryMenu;
import com.itheima.service.CategoryMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Description:
 *
 * @author chenzhi
 * @date 2020/8/3
 */
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/categoryMenu")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class CategoryMenuController {

    @Autowired
    private CategoryMenuService categoryMenuService;

    @PostMapping("/add")
    public Result add(@RequestBody TCategoryMenu tCategoryMenu) {
        try {
            String uid = UUID.randomUUID().toString().toLowerCase().replaceAll("-", "");
            tCategoryMenu.setUid(uid);
            categoryMenuService.add(tCategoryMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "插入数据失败");
        }
        return new Result("success", "插入数据成功");
    }

    @PostMapping("/delete")
    public ResultMessage delete(@RequestBody TCategoryMenu tCategoryMenu) {
        try {
            categoryMenuService.delete(tCategoryMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMessage(0,"error", "删除数据失败");
        }
        return new ResultMessage(1,"success", "删除数据成功");
    }

    @PostMapping("/edit")
    public ResultMessage edit(@RequestBody TCategoryMenu tCategoryMenu) {
        try {
            categoryMenuService.edit(tCategoryMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMessage(0,"error", "修改数据失败");
        }
        return new ResultMessage(1,"success", "修改数据成功");
    }

    @GetMapping("/getAll")
    public Result getAll(@RequestParam(name = "keyword",required = false) String keyword) {

        List<TCategoryMenu> resultList= null;
        try {
            resultList = categoryMenuService.getAll(keyword);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "操作失败");
        }
        return new Result(" success",resultList) ;

    }

    @GetMapping("/getButtonAll")
    //获取所有二级菜单-按钮列表
    public Result getButtonAll(@RequestParam("keyword") Integer keyword) {
        List<TCategoryMenu> tCategoryMenuList= null;
        try {
            tCategoryMenuList = categoryMenuService.getButtonAll(keyword);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "操作失败");
        }
        return new Result("success",tCategoryMenuList);

    }

    @PostMapping("/getList")
    //获取菜单列表
    public Result getList(@RequestBody Map<String, Object> searchMap) {
        Page<TCategoryMenu> pageList=categoryMenuService.getList(searchMap);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setTotal((int) pageList.getTotal());
        result.setRecords(pageList.getResult());
        return new Result("success",result) ;

    }

    @PostMapping("/stick")
    //置顶菜单
    public Result stick(@RequestBody TCategoryMenu tCategoryMenu) {
        try {
            categoryMenuService.stick(tCategoryMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "操作失败");
        }
        return new Result("success", "操作成功");
    }
}
